﻿using System;
using System.Data;
using ltModel;

public partial class ltMan_votes_voteAdd : AdminBasePage
{
    /*-_-!!!!!这是虾米代码*/
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string id = Request.QueryString["id"];
            if (Str.IsInt(id))
            {
                RadButton1.Visible = false;
                ltModel.BLL.Class cbl = new ltModel.BLL.Class();
                ltModel.BLL.vote vbl = new ltModel.BLL.vote();
                DataTable cdt = new DataTable();
                DataTable vdt = new DataTable();
                cdt = cbl.GetList("[id]=" + id).Tables[0];
                this.txt_title.Text = DataProtector.Getrep(cdt.Rows[0]["title"].ToString());
                this.kg.SelectedValue = cdt.Rows[0]["kg"].ToString();
                this.style.SelectedValue = cdt.Rows[0]["style"].ToString();
                if (!cdt.Rows[0].IsNull("overtime"))
                    endTime.DbSelectedDate = cdt.Rows[0]["overtime"];
                vdt = vbl.GetList("[class]=" + id).Tables[0];
                txt_count.Text = vdt.Rows.Count.ToString();
                for (int i = 0; i < vdt.Rows.Count; i++)
                {
                    switch (i)
                    {
                        case 0:
                            this.Text1.Value = vdt.Rows[i]["vote"].ToString();
                            this.Select1.Value = vdt.Rows[i]["color"].ToString();
                            break;
                        case 1:
                            this.Text2.Value = vdt.Rows[i]["vote"].ToString();
                            this.Select2.Value = vdt.Rows[i]["color"].ToString();
                            break;
                        case 2:
                            this.Text3.Value = vdt.Rows[i]["vote"].ToString();
                            this.Select3.Value = vdt.Rows[i]["color"].ToString();
                            break;
                        case 3:
                            this.Text4.Value = vdt.Rows[i]["vote"].ToString();
                            this.Select4.Value = vdt.Rows[i]["color"].ToString();
                            break;
                        case 4:
                            this.Text5.Value = vdt.Rows[i]["vote"].ToString();
                            this.Select5.Value = vdt.Rows[i]["color"].ToString();
                            break;
                        case 5:
                            this.Text6.Value = vdt.Rows[i]["vote"].ToString();
                            this.Select6.Value = vdt.Rows[i]["color"].ToString();
                            break;
                        case 6:
                            this.Text7.Value = vdt.Rows[i]["vote"].ToString();
                            this.Select7.Value = vdt.Rows[i]["color"].ToString();
                            break;
                        case 7:
                            this.Text8.Value = vdt.Rows[i]["vote"].ToString();
                            this.Select8.Value = vdt.Rows[i]["color"].ToString();
                            break;
                        case 8:
                            this.Text9.Value = vdt.Rows[i]["vote"].ToString();
                            this.Select9.Value = vdt.Rows[i]["color"].ToString();
                            break;
                        case 9:
                            this.Text10.Value = vdt.Rows[i]["vote"].ToString();
                            this.Select10.Value = vdt.Rows[i]["color"].ToString();
                            break;
                    }
                    lbl_ids.Text = lbl_ids.Text + vdt.Rows[i]["id"].ToString() + "|";
                }
            }
            else
                endTime.SelectedDate = DateTime.Now.AddYears(2);
        }
    }
    protected void RadButton1_Click(object sender, EventArgs e)
    {

    }

    protected void RadSave_Click(object sender, EventArgs e)
    {
        string val = "";
        string col = "";

        val = this.Text1.Value.Trim();
        val = val + "|" + this.Text2.Value.Trim();
        val = val + "|" + this.Text3.Value.Trim();
        val = val + "|" + this.Text4.Value.Trim();
        val = val + "|" + this.Text5.Value.Trim();
        val = val + "|" + this.Text6.Value.Trim();
        val = val + "|" + this.Text7.Value.Trim();
        val = val + "|" + this.Text8.Value.Trim();
        val = val + "|" + this.Text9.Value.Trim();
        val = val + "|" + this.Text10.Value.Trim();

        col = this.Select1.Value;
        col = col + "|" + this.Select2.Value;
        col = col + "|" + this.Select3.Value;
        col = col + "|" + this.Select4.Value;
        col = col + "|" + this.Select5.Value;
        col = col + "|" + this.Select6.Value;
        col = col + "|" + this.Select7.Value;
        col = col + "|" + this.Select8.Value;
        col = col + "|" + this.Select9.Value;
        col = col + "|" + this.Select10.Value;

        string[] str = val.Split(new Char[] { '|' });
        val = "";
        for (int i = 0; i < Int32.Parse(txt_count.Text); i++)
        {
            if (string.IsNullOrEmpty(str[i]))
            {
                infoPanel.AddErrorInfo("请填写选择项！");
                OperateLog.AddFailedLog(CurrentUser.user_id, Request.UserHostAddress, "投票管理-添加>请填写选择项！");
                return;
            }
            val = val + str[i].ToString() + "|";
        }

        string id = Request.QueryString["id"];
        bool inEdit = Str.IsInt(id);
        ltModel.BLL.Class cls = new ltModel.BLL.Class();
        ltModel.Model.Class cbo = null;
        if (inEdit)
            cbo = cls.GetModel(int.Parse(id));
        else
            cbo = new ltModel.Model.Class();

        cbo.title = DataProtector.ToSBC(txt_title.Text.Trim());
        cbo.time = DateTime.Now.ToString();
        cbo.style = style.SelectedValue;
        cbo.kg = Int32.Parse(kg.SelectedValue);
        cbo.overtime = endTime.SelectedDate.Value.ToString("yyyy-MM-dd");

        if (!inEdit)
        {
            int cid = cls.Add(cbo);
            if (cid > 0)
            {
                ltModel.Model.vote vbo = new ltModel.Model.vote();
                ltModel.BLL.vote vbl = new ltModel.BLL.vote();
                string[] vot = val.Split(new Char[] { '|' });
                string[] cor = col.Split(new Char[] { '|' });
                for (int i = 0; i < vot.Length; i++)
                {
                    if (vot[i] != "")
                    {
                        vbo.Class = cid;
                        vbo.id = 0;
                        vbo.Vote = vot[i];
                        vbo.color = cor[i];
                        vbo.count = 0;
                        vbl.Add(vbo);
                    }
                }
                infoPanel.AddSuccessInfo("添加成功！");
                OperateLog.AddFailedLog(CurrentUser.user_id, Request.UserHostAddress, "投票管理-添加>" + cbo.title);
            }
        }
        else
        {
            cls.Update(cbo);
            ltModel.Model.vote vbo = new ltModel.Model.vote();
            ltModel.BLL.vote vbl = new ltModel.BLL.vote();
            string[] vot = val.Split(new Char[] { '|' });
            string[] cor = col.Split(new Char[] { '|' });
            string[] ids = lbl_ids.Text.Split(new Char[] { '|' });
            for (int i = 0; i < vot.Length; i++)
            {
                if (vot[i] != "")
                {
                    vbo.Class = Int32.Parse(id);
                    vbo.id = Int32.Parse(ids[i]);
                    vbo.Vote = vot[i];
                    vbo.color = cor[i];
                    vbl.Update(vbo);
                }
            }
            infoPanel.AddSuccessInfo("修改成功！");
            OperateLog.AddFailedLog(CurrentUser.user_id, Request.UserHostAddress, "投票管理-修改>" + cbo.title);
        }
        AddScriptM("setTimeout(parent.reloadData, 300);");
    }
    protected void txt_count_TextChanged(object sender, EventArgs e)
    {

    }
}